package org.wattdepot.simulation.dorm.floor;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimerTask;
import org.wattdepot.client.WattDepotClient;

/**
 * A TimerTask that wraps a single FloorLoad instance and updates its power and energy and then
 * sends the results to WattDepot.  The task should be woken up every 10 seconds.  
 * @author Philip Johnson
 *
 */
public class FloorLoadTimerTask extends TimerTask {
  /** The FloorLoad of interest for this task. */
  private FloorLoad floor;
  /** The WattDepot client for this floor. */
  private WattDepotClient client;

  /**
   * Initializes this task with the floor and client. 
   * @param floor The FloorLoad.
   * @param client The WattDepotClient. 
   */
  public FloorLoadTimerTask(FloorLoad floor, WattDepotClient client) {
    this.floor = floor;
    this.client = client;
  }

  /**
   * Invoked every 10 seconds, this updates the Floor's power and energy, then sends the results
   * to WattDepot.
   */
  @Override
  public void run() {
    floor.update(getCurrentInterval());
    floor.sendData(client);
  }
  
  /**
   * Returns an integer between 0 and 8640 indicating the interval associated with the current time.
   * @return An integer between 0 and 8640.
   */
  private int getCurrentInterval() {
    GregorianCalendar now = new GregorianCalendar();
    int hourOfDay = now.get(Calendar.HOUR_OF_DAY);
    int minuteOfDay = now.get(Calendar.MINUTE);
    return (hourOfDay * 360) + (minuteOfDay * 6);
  }

}
